package com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItF5NetworkMap;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface F5NetworkMapRepo extends JpaRepository<ItF5NetworkMap, Integer> {

    @Transactional
    @Modifying
    @Query(value = "truncate table t_it_f5_network_map", nativeQuery = true)
    Integer truncateTable();

    @Query("select a from ItF5NetworkMap a where 1=1 " +
            "and ((:key is null or host like CONCAT('%',:key,'%')) " +
            "or (:key is null or vsName like  CONCAT('%',:key,'%')) " +
            "or (:key is null or partition like  CONCAT('%',:key,'%')) " +
            "or (:key is null or source like CONCAT('%',:key,'%')) " +
            "or (:key is null or destination like CONCAT('%',:key,'%')) " +
            "or (:key is null or port like CONCAT('%',:key,'%')) " +
            "or (:key is null or enable like CONCAT('%',:key,'%')) " +
            "or (:key is null or ipProtocol like CONCAT('%',:key,'%')) " +
            "or (:key is null or nat64 like CONCAT('%',:key,'%')) " +
            "or (:key is null or pool like CONCAT('%',:key,'%')) " +
            "or (:key is null or satType like CONCAT('%',:key,'%')) " +
            "or (:key is null or profileName like CONCAT('%',:key,'%')) " +
            "or (:key is null or poolMembers like CONCAT('%',:key,'%')) " +
            "or (:key is null or poolMembersState like  CONCAT('%',:key,'%'))) ")
    Page<ItF5NetworkMap> searchF5NetworkMap(Pageable pageable, String key);

    @Query("select a from ItF5NetworkMap a where 1=1 " +
            "and (:destination is null or destination = :destination ) " +
            "and (:port is null or port = :port ) " )
    List<ItF5NetworkMap> findByDestinationAndPort(String destination, String port);
}
